package com.shao.im.application;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@SpringBootApplication
@ComponentScan("com.shao")
@MapperScan("com.shao.im.dao")
@EnableAspectJAutoProxy
@EnableAsync
@EnableTransactionManagement
public class ImApplication {
    public static void main(String[] args) {
        SpringApplication.run(ImApplication.class, args);
    }


    @Bean
    public CorsFilter corsFilter() {
        // 初始化跨域过滤器配置
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(false);   // 是否允许cookie跨域
        corsConfiguration.addAllowedOrigin("*");    // 任意host来源
        corsConfiguration.addAllowedHeader("*");    // 任意请求头
        corsConfiguration.addAllowedMethod("*");    // 任意请求类型 POST/GET

        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        // 设置跨域的配置
        configurationSource.registerCorsConfiguration("/**", corsConfiguration);
        // 初始化并返回跨域过滤器
        return new CorsFilter(configurationSource);
    }
}
